using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Order.Dto;
using JTZY.Model.Order;
using JTZY.Repository;
using JTZY.Service.Order.IOrderService;
using JTZY.Model.Product;

namespace JTZY.Service.Order
{
    /// <summary>
    /// 换货明细Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IExchangedetailService), ServiceLifetime = LifeTime.Transient)]
    public class ExchangedetailService : BaseService<Exchangedetail>, IExchangedetailService
    {
        /// <summary>
        /// 查询换货明细列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<ExchangedetailDto> GetList(ExchangedetailQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<Exchangedetail, ExchangedetailDto>(parm);

            return response;
        }

        public List<ExchangedetailDto> GetExchangedetails(QueryDetailsDto parm) 
        {
            return Queryable().LeftJoin<Prodinfo>((it, prod) => it.SnCode == prod.SnCode)
                .LeftJoin<Corecmsbillaftersalesitem>((it, prod, item) => it.ProductId == item.ProductId && it.AftersalesId == item.AftersalesId)
                .Where((it, prod, item) => it.AftersalesId == parm.aftersalesId)
                .Select((it, prod, item) => new ExchangedetailDto {
                    ImageUrl=item.ImageUrl,
                    Addon=item.Addon,
                    SnCode=prod.SnCode,
                    productDate = prod.ProdctDate,
                    UserId=prod.UserId.HasValue? prod.UserId.Value:0,
                }).ToList();
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Exchangedetail GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加换货明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Exchangedetail AddExchangedetail(Exchangedetail model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改换货明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateExchangedetail(Exchangedetail model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Exchangedetail> QueryExp(ExchangedetailQueryDto parm)
        {
            var predicate = Expressionable.Create<Exchangedetail>();

            return predicate;
        }
    }
}